package com.ensah.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.ensah.bo.Etudiant;

public class SQLEtudiantDAO extends DAO<Etudiant> {

	/**
	 * Ordre SQL pour retrouver un �tudiant par id
	 */
	final private String findByID = "select * from etudiant where id = ?";

	public SQLEtudiantDAO(Connection pCon) {
		super(pCon);
	}

	@Override
	public void create(Etudiant pObj) throws DaoException {

	}

	@Override
	public boolean delete(Etudiant obj) throws DaoException {
		return false;
	}

	@Override
	public boolean update(Etudiant obj) throws DaoException {
		return false;
	}

	@Override
	public Etudiant find(int id) throws DaoException {

		Etudiant etd = null;
		try {
			PreparedStatement pst = connection.prepareStatement(findByID);
			pst.setInt(1, id);
			ResultSet rset = pst.executeQuery();
			etd = resultSetToStudent(rset);
		} catch (SQLException ex) {
			throw new DaoException(ex);
		}

		return etd;
	}

	private Etudiant resultSetToStudent(ResultSet rset) throws SQLException {
		Etudiant std = null;
		if (rset.first()) {
			std = new Etudiant(rset.getInt("id"), rset.getString("nom"),
					rset.getString("prenom"));

		}
		return std;
	}

}
